<script type="text/javascript">
	$(document).ready(function(){

		$('#petition').dataTable();
		
	});

	function paidCheque(cheque_no){
		var task = "paid";
		//alert(id);

			$.post('processPetition.php',{task: task, cheque_no: cheque_no}, function(data) {
					window.location.href="index.php?pages=paid";
			});

	}

	function approvedCheque(cheque_no){
		var task = "approvedCheque";
		//alert(task);
		/*
		$.post('processPetition.php',{task: task, petition_id: id}, function(data) {
			window.location.href="index.php?pages=finance";
		});
		*/
		
		$('<div>').dialog2({
			autoOpen: true,
			title: "อนุมัติ",
			content: "dlgDate.php?cheque_no=" + cheque_no + "&task=" + task,
			id: "modal-dlgDate"
		});
		


	}
	function disapprovalCheque(cheque_no){
		var task = "disapprovalCheque";

		$.post('processPetition.php',{task: task, cheque_no: cheque_no}, function(data) {
			window.location.href="index.php?pages=cheque";
		});

	}

</script>

<?php
	//echo "finance.php"
	include("includes/connect.php");

	$db = new myDBS;

	//$where = "petition_id = '$petition_id'";
	//$sql ="SELECT tbl_petition.*, pp_check_job.proID, projectName, amount, instalment FROM tbl_petition, //pp_check_job WHERE tbl_petition.ppID = pp_check_job.ppID and cheque_status = 'Approved'";
	$sql = "SELECT  cheque_no ,  bank_name ,  branch_name, cheque_status, paid_status, sum(inpNet) AS inpNet
		FROM  tbl_petition 
		WHERE  cheque_no IS NOT NULL 
		AND approved_status =  'Approved'
		GROUP BY  cheque_no ,  bank_name ,  branch_name, cheque_status, paid_status 
		ORDER BY  cheque_no
		";
	$result = $db->runSQL($sql);


?>
	<table id="petition" class="table table-striped" width="100%" >
		<caption><br></caption>
		<thead>
			<tr>
				<th>#</th>
				<th>เลขที่เช็ค</th>
				<th>ชื่อธนาคาร</th>
				<th>สาขา</th>
				<th>จำนวนเงิน</th>
				<th>รายละเอียดอื่น</th>
				<th>อนุมัติเช็ค</th>
				<th>จ่ายเช็ค</th>
			</tr>
		</thead>
		<tbody>
			<?php
				$i=0;
				while ( $rows = $db->fetch_object ( $result )){
					$i++;
			?>
			<tr>
				<td><?php echo $i; ?></td>
				<td><?php echo $rows->cheque_no; ?></td>
				<td><?php echo $rows->bank_name; ?></td>
				<td><?php echo $rows->branch_name; ?></td>
				<td><?php echo number_format($rows->inpNet, 2); ?></td>
				<td>
					<a href="#" id="info_<?php echo $rows->cheque_no; ?>"><i class="icon-info-sign"></i>&nbsp;ดู</a>
				</td>
				<td>
					<?php 
						if($rows->cheque_status <> "Not Approve") {
							echo $rows->cheque_status;
						} else {
					?>
					<a href="javascript:approvedCheque('<?php echo $rows->cheque_no; ?>')" data-toggle="tooltip" title="อนุมัติเช็ค"><i class="icon-ok"></i></a>&nbsp;
					<a href="javascript:disapprovalCheque('<?php echo $rows->cheque_no; ?>')" data-toggle="tooltip" title="ยกเลิกเช็ค"><i class="icon-remove"></i></a>
					<?php } ?>
				</td>
	 			<td>
					<?php 
						if ($rows->cheque_status == "Approved") {
						if($rows->paid_status <> "Not Paid") {
							echo $rows->paid_status;
						} else {
					?>
					<a href="javascript:paidCheque('<?php echo $rows->cheque_no; ?>')" data-toggle="tooltip" title="จ่ายเช็ค"><i class="icon-ok"></i></a>
					<?php 
						} 
						} else {
							echo "รอสถานะเช็ค";
						}?>
				</td>
				
			</tr>
			<?php
					
				}
			?>

		</tbody>


	</table>
